package minh.bac.wcbrazil2014.listviewmatch;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import minh.bac.wcbrazil2014.R;
import minh.bac.wcbrazil2014.WCTAG;
import minh.bac.wcbrazil2014.adapter.VongBangAdapter;
import minh.bac.wcbrazil2014.database.MySQLiteManager;
import minh.bac.wcbrazil2014.object.MatchScheduleObj;
import minh.bac.wcbrazil2014.object.VongBangObj;
import minh.bac.wcbrazil2014.object.VongDau;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

public class ListViewFragment extends Fragment implements OnChildClickListener,WCTAG{
	
	ExpandableListView expandableListViewVB;
	VongBangAdapter vongBangAdapter;
	private MySQLiteManager mMySQLiteManager;	
	ArrayList<VongBangObj> arrayVongBangObjs;	
	private LinkedHashMap<String, ArrayList<MatchScheduleObj>> mMapDataExpandListview;
	private ArrayList<String> mDataHeader;
	ArrayList<VongDau> arrayVongDauObjs;
	
	DisplayImageOptions displayImageOptions;
	private int mTypeShow;
	public static ListViewFragment newsInstance(int typeShow)
	{
		ListViewFragment listViewFragment = new ListViewFragment();
		Bundle bundle = new Bundle();
		bundle.putInt(WCTAG.TYPE_SHOW, typeShow);
		listViewFragment.setArguments(bundle);
		return listViewFragment;
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		Log.d("LISTVIEW", "listview createview");
		//
		mTypeShow =  getArguments().getInt(WCTAG.TYPE_SHOW);
		//getActivity().getSupportFragmentManager().beginTransaction().addToBackStack(WCTAG.TAGFM_VONGBANG).commit();
		
		View rootView = inflater.inflate(R.layout.listview_vongbang, container, false);
		mMySQLiteManager = new MySQLiteManager(getActivity());
		expandableListViewVB = (ExpandableListView) rootView.findViewById(R.id.listview_vongbangELV);		
		mDataHeader = new ArrayList<String>();
		displayImageOptions = new DisplayImageOptions.Builder()
		.cacheInMemory(false)
		.cacheOnDisc(true)		
		.resetViewBeforeLoading(false)
		.bitmapConfig(Bitmap.Config.RGB_565)										
		.showImageForEmptyUri(R.drawable.ic_emblem_club_placeholder)
		.showImageOnFail(R.drawable.ic_emblem_club_placeholder)		
		.imageScaleType(ImageScaleType.EXACTLY)										
		.build();	
		//		
		mMapDataExpandListview = new LinkedHashMap<String, ArrayList<MatchScheduleObj>>();
		//
		LoadData();
		//
		vongBangAdapter = new VongBangAdapter(getActivity(), mDataHeader, mMapDataExpandListview, displayImageOptions, mTypeShow);
		expandableListViewVB.setAdapter(vongBangAdapter);
		//
		expandableListViewVB.setOnChildClickListener(this);
		return rootView;
	}
	/**
	 * this method use to load data
	 */
	public void LoadData()
	{
		//
		switch (mTypeShow) {
		case TYPE_SHOW_VONGBANG: 
			arrayVongBangObjs = new ArrayList<VongBangObj>();
			arrayVongBangObjs = mMySQLiteManager.getAllFromVongBang();
			if(arrayVongBangObjs != null)
			{
				mMapDataExpandListview = changeToHashMap(arrayVongBangObjs);
				for(String key : mMapDataExpandListview.keySet())
				{
					
					mDataHeader.add(key);
				}
			}
			else
			{
				Toast.makeText(getActivity(), "Failed", Toast.LENGTH_SHORT).show();
			}
			break;
		case TYPE_SHOW_VONG18:
			arrayVongDauObjs = new ArrayList<VongDau>();
			arrayVongDauObjs  = mMySQLiteManager.getVong18();
			if(arrayVongDauObjs != null)
			{
				mMapDataExpandListview = changeToHashMapVD(arrayVongDauObjs);
				for(String key : mMapDataExpandListview.keySet())
				{
					mDataHeader.add(key);
				}
			}
			else
			{
				Toast.makeText(getActivity(), "Failed", Toast.LENGTH_SHORT).show();
			}
			break;
		case TYPE_SHOW_VONGTUKET:
			arrayVongDauObjs = new ArrayList<VongDau>();
			arrayVongDauObjs  = mMySQLiteManager.getVongTuKet();
			if(arrayVongDauObjs != null)
			{
				mMapDataExpandListview = changeToHashMapVD(arrayVongDauObjs);
				for(String key : mMapDataExpandListview.keySet())
				{
					mDataHeader.add(key);
				}
			}
			else
			{
				Toast.makeText(getActivity(), "Failed", Toast.LENGTH_SHORT).show();
			}
			break;
		case TYPE_SHOW_VONGBANKET:
			arrayVongDauObjs = new ArrayList<VongDau>();
			arrayVongDauObjs  = mMySQLiteManager.getVongBanKet();
			if(arrayVongDauObjs != null)
			{
				mMapDataExpandListview = changeToHashMapVD(arrayVongDauObjs);
				for(String key : mMapDataExpandListview.keySet())
				{
					mDataHeader.add(key);
				}
			}
			else
			{
				Toast.makeText(getActivity(), "Failed", Toast.LENGTH_SHORT).show();
			}
			break;
		case TYPE_SHOW_VONGGIAI34:
			arrayVongDauObjs = new ArrayList<VongDau>();
			arrayVongDauObjs  = mMySQLiteManager.getVong34();
			if(arrayVongDauObjs != null)
			{
				mMapDataExpandListview = changeToHashMapVD(arrayVongDauObjs);
				for(String key : mMapDataExpandListview.keySet())
				{
					mDataHeader.add(key);
				}
			}
			else
			{
				Toast.makeText(getActivity(), "Failed", Toast.LENGTH_SHORT).show();
			}
			break;
		case TYPE_SHOW_VONGCHUNGKET:
			arrayVongDauObjs = new ArrayList<VongDau>();
			arrayVongDauObjs  = mMySQLiteManager.getVonChungket();
			if(arrayVongDauObjs != null)
			{
				mMapDataExpandListview = changeToHashMapVD(arrayVongDauObjs);
				for(String key : mMapDataExpandListview.keySet())
				{
					mDataHeader.add(key);
				}
			}
			else
			{
				Toast.makeText(getActivity(), "Failed", Toast.LENGTH_SHORT).show();
			}
			break;

		default:
			break;
		}
		//move arraylist to hashmap
		

	
	}
	
	public LinkedHashMap<String, ArrayList<MatchScheduleObj>> changeToHashMap(ArrayList<VongBangObj> arrayList)
	{
		LinkedHashMap<String, ArrayList<MatchScheduleObj>> hashMap = new LinkedHashMap<String, ArrayList<MatchScheduleObj>>();
		if(arrayList != null && arrayList.size() > 0)
		{
			String key = arrayList.get(0).getNgay();
			ArrayList<String> keys = new ArrayList<String>();
			keys.add(key);
			for(int i = 1; i < arrayList.size(); i++)
			{
				String item  = arrayList.get(i).getNgay();
				if(!keys.contains(item))
				{
					keys.add(item);
				}
			}
			// add map
			for(String keyMap: keys)
			{
				ArrayList<MatchScheduleObj> temp = new ArrayList<MatchScheduleObj>();
				for(int i = 0; i < arrayList.size(); i++)
				{
					if( keyMap.equals(arrayList.get(i).getNgay()) )
					{
						temp.add(arrayList.get(i));
					}
				}
				hashMap.put(keyMap, temp);
			}
		}
		return hashMap;
	}
	public LinkedHashMap<String, ArrayList<MatchScheduleObj>> changeToHashMapVD(ArrayList<VongDau> arrayList)
	{
		LinkedHashMap<String, ArrayList<MatchScheduleObj>> hashMap = new LinkedHashMap<String, ArrayList<MatchScheduleObj>>();
		if(arrayList != null && arrayList.size() > 0)
		{
			String key = arrayList.get(0).getNgay();
			ArrayList<String> keys = new ArrayList<String>();
			keys.add(key);
			for(int i = 1; i < arrayList.size(); i++)
			{
				String item  = arrayList.get(i).getNgay();
				if(!keys.contains(item))
				{
					keys.add(item);
				}
			}
			// add map
			for(String keyMap: keys)
			{
				ArrayList<MatchScheduleObj> temp = new ArrayList<MatchScheduleObj>();
				for(int i = 0; i < arrayList.size(); i++)
				{
					if( keyMap.equals(arrayList.get(i).getNgay()) )
					{
						temp.add(arrayList.get(i));
					}
				}
				hashMap.put(keyMap, temp);
			}
		}
		return hashMap;
	}
	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		// TODO Auto-generated method stub
		ItemListViewFragment fragment = ItemListViewFragment.newInstance(mTypeShow);
		MatchScheduleObj matchScheduleObj = mMapDataExpandListview.get(mDataHeader.get(groupPosition)).get(childPosition);
		fragment.setMatchScheduleObj(matchScheduleObj);
		ListViewMatchFragment.switchFragment(R.id.container_viewpager_lichdau, fragment, true, null);
		//HomeScreen.switchFragment(R.id.container, new , addToBackStack, TAG);
		return true;
	}
	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Log.d("LISTVIEW", "listview stop");
	}
	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
		super.onDestroyView();
		Log.d("LISTVIEW", "listview destoy");
	}
}
